/*
 * Powered By [rapid-framework]
 * Web Site: http://www.rapid-framework.org.cn
 * Google Code: http://code.google.com/p/rapid-framework/
 * Since 2008 - 2011
 */

package com.tech15.core.service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javacommon.base.BaseManager;
import javacommon.base.EntityDao;
import javacommon.util.CtgData;
import javacommon.util.IndexTab;
import javacommon.util.org.TreeUtils;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.org.rapid_framework.page.Page;

import com.tech15.core.dao.ModuleDao;
import com.tech15.core.dao.SearchDataDao;
import com.tech15.core.dao.SysModuleDataDao;
import com.tech15.core.dao.SysUserDao;
import com.tech15.core.model.Module;
import com.tech15.core.model.SysModuleData;
import com.tech15.core.model.SysUser;
import com.tech15.core.vo.query.ModuleQuery;

/**
 * @author 胡作正 email:zuozhenghu@sina.com
 * @version 1.0
 * @since 1.0
 */

@Service
@Transactional
public class ModuleManager extends BaseManager<Module,Long>{

	private ModuleDao moduleDao;
	private SysUserDao sysUserDao;
	private SysModuleDataDao sysModuleDataDao;
	
	public void setSysModuleDataDao(SysModuleDataDao dao) {
		this.sysModuleDataDao = dao;
	}
	public void setSysUserDao(SysUserDao dao) {
		this.sysUserDao = dao;
	}
	public void setModuleDao(ModuleDao dao) {
		this.moduleDao = dao;
	}
	public EntityDao getEntityDao() {
		return this.moduleDao;
	}
	
	@Transactional(readOnly=true)
	public Page findPage(ModuleQuery query) {
		return moduleDao.findPage(query);
	}
	
	@Transactional(readOnly=true)
	public List<CtgData> findModuleTree(Integer appid) {
		List<CtgData> ctgDataList = new ArrayList<CtgData>();
		List<Module> list = moduleDao.findAllParentByAuthLayer2(appid);
		for(Module mod: list){
			Module module = moduleDao.getById(mod.getMdid());
			CtgData ctg = TreeUtils.setModuleContent(module);
			ctgDataList.add(ctg);
		}
		return ctgDataList;
	}
	public List<CtgData> findAuthModuleTree(Long authid,String authType,Integer appid) {
		List<Module> list = moduleDao.findAuthModuleTree(authid,authType,appid);
		return TreeUtils.setModuleContent(list);
	}
	public List<CtgData> userFinalModuleTree(Long userid) {
		List<Module> list = moduleDao.userFinalModuleTree(userid);
		return TreeUtils.setFinalModuleContent(list);
	}
	
	
	/**查询所有固定权限*/
	public Map findAllFixFunByUserId(Long userid,Integer appid) {
		Map map = Collections.synchronizedMap(new LinkedHashMap());
		
		/*正常角色权限获取*/
		//固定模块-父
		List<Module> z_parent = moduleDao.findAllParentFunByUserId(userid,appid);
		//所有授权模块-子
		List<Module> z_child = moduleDao.findAllFunByUserId(userid);
		for(Module m : z_parent){
			List<IndexTab> list = new ArrayList<IndexTab>();
			for(Module mod : z_child){
				if(mod.getPid().longValue() == m.getMdid().longValue()){
					IndexTab tab = new IndexTab();
					tab.setTabTitle(mod.getName());
					tab.setUrl(mod.getUrl());
					list.add(tab);
				}
			}
			map.put(m.getName(), list);
		}
		
		SysUser sysUser = sysUserDao.getById(userid);
		/*超级管理员*/
		if(sysUser.getIsadmin() == 1){
			List<Module> parent = moduleDao.findAllSysAdminFun();
			for(Module m : parent){
				List<IndexTab> list = new ArrayList<IndexTab>();
				Set children = m.getModules();
				for (Iterator iter = children.iterator(); iter.hasNext();){
					Module mod = (Module)iter.next();
					if(mod.getPid().longValue() == m.getMdid().longValue()){
						IndexTab tab = new IndexTab();
						tab.setTabTitle(mod.getName());
						tab.setUrl(mod.getUrl());
						list.add(tab);
					}
				}
				map.put(m.getName(), list);
			}
		}
		/*客户管理员*/
		else if(sysUser.getIsadmin() == 1111){
			List<Module> parent = moduleDao.findAllAdminFun();
			for(Module m : parent){
				List<IndexTab> list = new ArrayList<IndexTab>();
				Set children = m.getModules();
				for (Iterator iter = children.iterator(); iter.hasNext();){
					Module mod = (Module)iter.next();
					if(mod.getPid().longValue() == m.getMdid().longValue()){
						IndexTab tab = new IndexTab();
						tab.setTabTitle(mod.getName());
						tab.setUrl(mod.getUrl());
						list.add(tab);
					}
				}
				map.put(m.getName(), list);
			}
		}
		/*普通员工获取权限放在方法的最前面*/
		/*else{
			//固定模块-父
			List<Module> parent = moduleDao.findAllParentFunByUserId(username,"1");
			//所有授权模块-子
			List<Module> child = moduleDao.findAllFunByUserId(username);
			for(Module m : parent){
				List<IndexTab> list = new ArrayList<IndexTab>();
				for(Module mod : child){
					if(mod.getPid().longValue() == m.getMdid().longValue()){
						IndexTab tab = new IndexTab();
						tab.setTabTitle(mod.getName());
						tab.setUrl(mod.getUrl());
						list.add(tab);
					}
				}
				map.put(m.getName(), list);
			}
		}*/
		
		return map;
	}
	public void saveSubs(Long[] subs,Long pid,Integer appid) {
		int index = 1;
		for(Long mdid : subs){
			SysModuleData smd = sysModuleDataDao.getById(mdid);
			Module m = new Module();
			m.setName(smd.getName());
			m.setMdno(smd.getMdno());
			m.setRemark(smd.getRemark());
			m.setLayer(2);
			m.setPid(pid);
			m.setUrl(smd.getUrl());
			m.setFixed("1");
			m.setType(0);
			m.setSortno(index);
			m.setAppid(appid);
			m.setIsshfw(0);
			moduleDao.save(m);
			
			index++;
		}
	}
	public List<CtgData> loadModuleTree(Integer appid) {
		List<CtgData> tdDataList = new ArrayList<CtgData>();
		List<Module> pds = moduleDao.findAllByAppid(appid);
		for(Module d : pds){
			CtgData td = TreeUtils.setModuleContent(d);
			tdDataList.add(td);
		}
		return tdDataList;
	}
	public List<Module> listByPid(Long pid) {
		return moduleDao.findAllByPid(pid);
	}
	
	public Integer getModuleCount(Long userid,Integer appid){
		return moduleDao.getModuleCount(userid, appid);
	}
	
}
